﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Business;

public partial class User_Shares_SurveyorList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
            q.QueryType = SubSonic.QueryType.Select;
            q.AND(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhoghooghi");
            q.OR(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhaghighi");
            q.OR(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyor");

            GridViewSurveyors.DataSource = q.ExecuteDataSet();
            GridViewSurveyors.DataBind();
        }
        catch
        {

        }
    }
    protected void GridViewSurveyors_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {





                Guid USerid = new Guid(e.Row.Cells[18].Text);

                ProfileCommon profile = Profile.GetProfile(Membership.GetUser(USerid).UserName);

                string p = e.Row.Cells[6].Text;
                if (p == "Surveyorhoghooghi")
                {
                    if (profile.HoghooghiAdvisor.CompanyRegistrationCode != "")
                        e.Row.Cells[8].Text = profile.HoghooghiAdvisor.CompanyRegistrationCode;
                    else
                        setnull(8, e);
                    if (profile.HoghooghiAdvisor.ValidationDate != "")
                    {
                        string pdate = new Business.Calender().MiladiToPersian(Convert.ToDateTime(profile.HoghooghiAdvisor.ValidationDate));
                        e.Row.Cells[9].Text = pdate;
                    }
                    else
                        setnull(9, e);
                    if (profile.HoghooghiAdvisor.Branch != "")
                        e.Row.Cells[12].Text = profile.HoghooghiAdvisor.Branch;
                    else
                        setnull(12, e);

                    if (profile.HoghooghiAdvisor.ExtendedBranch != "")
                        e.Row.Cells[13].Text = profile.HoghooghiAdvisor.ExtendedBranch;
                    else
                        setnull(13, e);

                    e.Row.Cells[6].Text = " نقشه بردار حقوقی";
                }
                if (p == "Surveyorhaghighi")
                {
                    if (profile.HaghighiAdvisor.Branch != "")
                        e.Row.Cells[12].Text = profile.HaghighiAdvisor.Branch;
                    else
                        setnull(12, e);
                    if (profile.HaghighiAdvisor.GraduateGrade != "")
                        e.Row.Cells[14].Text = profile.HaghighiAdvisor.GraduateGrade;
                    else
                        setnull(14, e);
                    if (profile.HaghighiAdvisor.GraduateDate != "")
                    {
                        string pdate = new Business.Calender().MiladiToPersian(Convert.ToDateTime(profile.HaghighiAdvisor.GraduateDate));
                        e.Row.Cells[15].Text = pdate;
                    }
                    else
                        setnull(15, e);

                    e.Row.Cells[6].Text = " نقشه بردار حقیقی";
                }
                 if (p == "Surveyor")
                     e.Row.Cells[6].Text = " نقشه بردار ";
                e.Row.Cells[16].Text = profile.Common.WorkYears;
                if (profile.Common.WorkYears == "")
                    setnull(16, e);
                e.Row.Cells[17].Text = profile.Common.City;
                if (profile.Common.City == "")
                    setnull(17, e);
                e.Row.Cells[10].Text = profile.Contacts.Email;
                if (profile.Contacts.Email == "")
                    setnull(10, e);
                e.Row.Cells[11].Text = profile.Contacts.Site;
                if (profile.Contacts.Site == "")
                    setnull(11, e);
                e.Row.Cells[18].Visible = false;

            }
        }
        catch
        {
        }

    }
    public void setnull(int row, GridViewRowEventArgs e)
    {
        e.Row.Cells[row].Text = "---";
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void ExportToExcel_Click(object sender, EventArgs e)
    {

        try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            this.EnableViewState = false;
            StringWriter sr = new StringWriter();
            HtmlTextWriter hr = new HtmlTextWriter(sr);
            GridViewSurveyors.RenderControl(hr);
            Response.Write(sr.ToString());
            sr.Dispose();
            hr.Dispose();
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }

    protected void GridViewSurveyors_PreRender(object sender, EventArgs e)
    {
        if (GridViewSurveyors.Rows.Count > 0)
        {
            //This replaces <td> with <th> and adds the scope attribute
            GridViewSurveyors.UseAccessibleHeader = true;

            //This will add the <thead> and <tbody> elements
            GridViewSurveyors.HeaderRow.TableSection = TableRowSection.TableHeader;

            //This adds the <tfoot> element. 
            //Remove if you don't have a footer row
            GridViewSurveyors.FooterRow.TableSection = TableRowSection.TableFooter;
        }


    }
}